package com.situ.shoplook.dubbo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.situ.shoplook.common.util.WebAppContext;
import com.situ.shoplook.dubbo.dao.ProductSpuDao;
import com.situ.shoplook.common.model.ProductSpu;
import com.situ.shoplook.common.model.search.ProductSpuSearchBean;
import com.situ.shoplook.common.service.ProductSpuService;
import com.situ.shoplook.common.util.PaginateParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 功能：
 *
 * @author 千堆雪
 * @version 1.0.0
 * <p>
 * created by 千堆雪 on 2024/8/25, last modified by 千堆雪 on 2024/8/25
 */
@Service
public class ProductSpuServiceImpl implements ProductSpuService {
    private ProductSpuDao productSpuDao;

    @Autowired
    public void setProductSpuDao(ProductSpuDao productSpuDao) {
        this.productSpuDao = productSpuDao;
    }

    @Override
    public List<ProductSpu> findAll(ProductSpuSearchBean gsb, PaginateParams pp) {
        try (Page<?> __ = PageHelper.startPage(pp.getPageNo(), pp.getPageSize())) {
            return this.productSpuDao.findAll(gsb);
        }
    }

    @Override
    public ProductSpu findById(Integer id) {
        return this.productSpuDao.findById(id);
    }

    @Override
    public boolean save(ProductSpu spu) {
        spu.setCreatedTime(LocalDateTime.now());
        spu.setUpdatedTime(LocalDateTime.now());
        if (WebAppContext.getCurrentAccount() != null) {
            spu.setCreatedBy(WebAppContext.getCurrentAccount().getUsername());
            spu.setUpdatedBy(WebAppContext.getCurrentAccount().getUsername());
        }
        return this.productSpuDao.save(spu) > 0;
    }

    @Override
    public boolean update(ProductSpu spu) {
        spu.setUpdatedTime(LocalDateTime.now());
        if (WebAppContext.getCurrentAccount() != null) {
            spu.setUpdatedBy(WebAppContext.getCurrentAccount().getUsername());
        }
        return this.productSpuDao.update(spu) > 0;
    }

    @Override
    public int deleteByIds(Integer[] ids) {
        return this.productSpuDao.deleteByIds(ids);
    }
}
